import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

public class Monster extends Canvas implements Thing {

	private Strategy strategy;
		
	private Thing maze[][];
	
	private Pacman p;
	
	private String direction;
	
	private int x;
	
	private int y;
	
	private int oldX;
	
	private int oldY;
	
	private boolean behaviour;
	
	private int width;
	
	public Monster(int x, int y, Strategy s) {
		this.x = x;
		this.y = y;
		oldX = x;
		oldY = y;
		behaviour = true;
		width=30;
		strategy = s;
	}
    
	public void hide(Graphics g) {
		g.setColor(Color.white);
		g.fillRect(oldX * 30, oldY * 30, 30, 30);
		if (maze[oldX][oldY] == null) {
			return;
		}
		maze[oldX][oldY].draw(g);
	}
	
	/**
	 * drawing of monster
	 */
	public void draw(Graphics g) {
		g.setColor(Color.DARK_GRAY);
		g.fillRoundRect(x*width+0, y*width+0, width, width, (int)(0.66*width), width);
		g.fillRect(x*width+0, y*width+(int)(0.33*width), width, (int)(0.66*width));
		g.setColor(Color.white);
		g.fillOval(x*width+2,y*width+(int)(0.83*width),(int)(0.21*width),(int)(0.21*width));
		g.fillOval(x*width+(int)(0.27*width),y*width+(int)(0.83*width),(int)(0.21*width),(int)(0.21*width));
		g.fillOval(x*width+(int)(0.52*width),y*width+(int)(0.83*width),(int)(0.21*width),(int)(0.21*width));
		g.fillOval(x*width+(int)(0.77*width),y*width+(int)(0.83*width),(int)(0.21*width),(int)(0.21*width));
		g.setColor(Color.black);
 		g.fillArc(x*width+(int)(0.2*width),y*width+(int)(0.13*width), (int)(0.66*width), (int)(0.66*width), 0, 45);
 		g.fillArc(x*width+(int)(0.16*width),y*width+(int)(0.13*width), (int)(0.66*width), (int)(0.66*width), 135, 45);
		g.setColor(Color.red);
		g.fillOval(x*width+(int)(0.26*width), y*width+(int)(0.33*width), 4, 4);
		g.fillOval(x*width+(int)(0.63*width), y*width+(int)(0.33*width), 4, 4);
		oldX = x;
		oldY = y;
		move();
	}
    
	/**
	 * method of checking pacmans and monsters coordinates
	 * @param xP - pacmans coordinate
	 * @param yP - pacmans coordinate
	 * @return true - if pacmans and monsters coordinates are equal, false - else
	 */
    public boolean ifFail(int xP, int yP) {
    	return ((x == xP ) && (y == yP)); 
    }
    
    /**
     * maze init
     * @param map - input data
     * @param p - instance of pacman class
     */
    public void init(Thing map[][], Pacman p) {
    	maze = map;
    	this.p = p;
    }
    
    /**
     * describe monsters movement
     */
    private void move() {
       	direction = strategy.SelectDirection(maze, x, y, p.getX(), p.getY());
       	if (direction == "Up")
       		y--;
       	if (direction == "Right")
       		x++;
		if (direction == "Down")
			y++;
		if (direction == "Left") 
			x--;
    }
}